Ext.namespace("Task.UpFile");

Task.UpFile.panel = function() {
	this.exeId;
	this.upFileNum=1;
	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../task/searchTaskUpLoadFile.form?timesnap=' +new Date().getTime()
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			fields: ['BUIS_ID','ACCESSORY_ID', 'ACCESSORY_NAME', 'UPLOAD_DATE','FILE_SIZE','UPLOAD_NAME','ACCESSORY_DESC','USER_NAME']
		}),listeners:{
			 beforeload :{    
	    	  	fn : function(thiz,options){
       	          Ext.apply(thiz.baseParams,
       	          { 
       	          	exeId:this.exeId,
       	          	limit:5
       	          });    
              },
              scope:this 
	    	}
		}
	});
/*	this.upLoadfile = new Ext.form.FileUploadField({
		fieldLabel : '上传附件',
		emptyText: '请选择要上传的文件',
		width : 420,
		name : 'fileName',
		buttonText: '',
		buttonCfg: {
            iconCls: 'icon-compress'
        }
	});*/
/*	this.upLoadfile2 = new Ext.form.FileUploadField({
		fieldLabel : '上传附件',
		emptyText: '请选择要上传的文件',
		width : 420,
		name : 'fileName',
		buttonText: '',
		buttonCfg: {
            iconCls: 'icon-compress'
        }
	});*/
	/*var upLoadFile = new Ext.Toolbar.Button({
	        iconCls: 'icon-accept',
	        text: '上传',
	        tooltip: {text: '上传'},
	        handler: this.saveTaskFile,
	        scope:this
	    });*/
	this.addNewFile = new Ext.Toolbar.Button({
        iconCls: 'icon-add',
        text: '添加附件',
        tooltip: {text: '添加附件'},
        handler: this.addNewUpFile,
        scope:this
    });
	this.fileForm = new Ext.FormPanel({
		bodyStyle: 'padding: 1px 0px 0',
        frame:true,
        fileUpload: true, 
        width:650,
        height:150,
        autoScroll: true,
        layout:'form',
        items : []     
    });
	var delTaskUploadFile = new Ext.data.Connection({
		url:'../task/delTaskUploadFile.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	this.renderHref= function(value){
		return value+"K";
	};
	this.colModel = new Ext.grid.ColumnModel({
	    defaults: {
	        width: 100,
	        sortable: true,
	        align: 'center'
	    },
	    columns: [
			{header: 'TASK_ID', width: 50, hidden: true, dataIndex: 'BUIS_ID'},
			{header: '附件名称', width: 100, dataIndex: 'ACCESSORY_NAME'},
			{header: '附件大小(K)', width: 100, dataIndex: 'FILE_SIZE',render:this.renderHref},
			{header: '附件上传时间',width: 200, dataIndex: 'UPLOAD_DATE'},
			{header: '备注',width: 100, dataIndex: 'ACCESSORY_DESC'},
			{header: '上传人',width: 100, dataIndex: 'USER_NAME'},
			{header: '操作',width: 50, dataIndex: 'ACCESSORY_ID',renderer:function (){return "<img src='../resources/icons/cancel.png' style='cursor:hand'></img>";}},/*<a href='#' style='cursor:hand'>删除</a>*/
			{header: '下载',width: 50, dataIndex: 'ACCESSORY_ID',renderer:this.downloadUrl}
	    ]
	});
	
	this.gridPanel = new Ext.grid.GridPanel({
		region: 'center',
		cm:this.colModel,
		height:180,
		store:this.store,
		stripeRows: true, 
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
        loadMask: {msg: '加载中，请稍候...'},
        bbar:new Ext.PagingToolbar({
			 pageSize: 5,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录...",
	         items:[this.addNewFile]
		}),
		
        viewConfig: { forceFit: true },
        listeners:{
        	cellclick:function (grid, rowIndex, columnIndex, e) {
	    		if(columnIndex==6){
	    			Ext.Msg.confirm("信息","确认删除?",function(btn){if(btn=='yes'){
	    				var record = grid.getStore().getAt(rowIndex);
		        		var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
		        		var data = record.get(fieldName);
		        		var buis_id = data;
		        		var task_id = record.get("BUIS_ID");

		        		Utils.request(delTaskUploadFile,{
		        			params:{
		        			buis_id:buis_id,
		        			task_id:task_id
		        			},
		        			success:function(){
		        				Utils.info('删除成功！');
		        				Task.Assign.panel.instance.store.reload();
		        				grid.store.reload();
		        				
		        			}
		        		});
		    			
		    			
	    			}else{return false;}});
	    		}else{
	    			return false;
	    		}
        	} 
        }
	});
	Task.UpFile.panel.superclass.constructor.call(this, {
		title:'附件信息',
		autoHeight:true,
		//layout:'border',
		items:[this.gridPanel,this.fileForm],
		listeners:{
			render:this.queryUpfile
			//activate:this.hiddenButton,
		}
	});
};
Ext.extend(Task.UpFile.panel, Ext.Panel, {
	initFeedBack:function(exeId){
	
		this.exeId = exeId;
/*		this.gridPanel.removeListener(cellclick);
		this.colModel.columns[4].renderer=function(){
			return "<a href='#'>删除</a>";
		};*/
		//console.info(this.colModel.columns[4].renderer);
	},
	queryUpfile :function(exeId){
		
		this.store.load({
			params:{
				start : 0,
				limit : 5,
				exeId:this.exeId
			}
		});
	},
	saveTaskFile :function(){
		scope=this;
		var flag = this.exeId;
		if(flag==null){
			Utils.info("请先保存任务基本信息","系统提示");
		}else{
			//console.info(this.fileForm.items.items[1].items.items[0].items.items[0].getValue());
			if(this.fileForm.items.items[1]){
				if(this.fileForm.items.items[1].items.items[0].items.items[0].getValue()==''){
					Utils.info("请选择要上传的附件","系统提示");
				}else{
					this.fileForm.getForm().submit({
					waitTitle : '系统提示',
					waitMsg : '数据提交中，请稍候...',
					url:'../task/addTaskFile.form?timesnap=' +new Date().getTime(),
					method:'post',
					params:{
						taskId:this.exeId,
						fileRemark:'派发附件'
					},
					success:function(){
						scope2= scope;//this,
						Utils.info("操作成功","系统提示");
						scope2.fileForm.getForm().reset();
						scope2.store.load({
							params:{
								start : 0,
								limit : 5,
								exeId:flag
							}
						});
						Task.Assign.panel.instance.store.reload();
						
	    				//grid.store.reload();
					},
					failure:function(){
						Utils.info("操作失败","系统提示");
					}
				});
				}
		}else{
			Utils.info("请点击添加并选择要上传的附件","系统提示");
		}
	}
	},
	delUpfile :function(s, m, rec){
		return "<a href=javascript:{del("+s+");function del(value){alert(value);}}>删除</a>";
	},
	addNewUpFile :function(){
		var upLoadfile = new Ext.form.FileUploadField({
			fieldLabel : '上传附件',
			emptyText: '请选择要上传的文件',
			width : 420,
			name : 'fileName'+this.upFileNum,
			buttonText: '',
			buttonCfg: {
	            iconCls: 'icon-compress'
	        }
		});
		var delFile = new Ext.Toolbar.Button({
			iconCls: 'icon-remove',
	        text: '删除',
	        tooltip: {text: '删除'},
	        handler: this.delFile,
	        scope:this
	    });
		var item={
				layout: 'column', border: false, 
				defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: .86, items: [upLoadfile]},
	        	         { columnWidth: .14, items: [delFile] }
	        	       ]
	    };
		this.fileForm.add(item);
		this.fileForm.doLayout();
		this.upFileNum = this.upFileNum+1;
	},
	delFile:function(btn,event){
		var item=btn.ownerCt.ownerCt;
		this.fileForm.remove(item,true);
	    this.fileForm.doLayout();
	}, 
	loadFile :function(){
		this.store.reload();
	},
	downloadUrl :function(s, m, rec){
		return "<a href='../task/downloadFile.form?fileId="+s+"'>下载</a>";
	}
});


